import Taro, { Component, useState, useEffect, useRouter } from '@tarojs/taro';
import { View, Input, Image } from '@tarojs/components';
// import Bg from './img/bg.png';
import Title from './img/title.png';
// import CouponTitle from './img/coupon-title.png';
import classnames from 'classnames';
// import Tips from '../../../components/tips-modal';
import NavigationBar from '../../../components/navigationBar/index';
// import { set as setGlobalData, get as getGlobalData } from '../../../utils/globalData';
// import { timeFormat, fullImgUrl } from '../../../utils/utils';

import './index.scss';
import { couponBind } from '../../../services/user';

function Exchange(props) {
  const navigationBarHeight = Taro.getStorageSync('navigationBarHeight');
  const [password, setPassword] = useState('');
  const [isTip, setIstip] = useState(Taro.getStorageSync('setting').is_tip);
  const [nickname, setNickname] = useState(''); // 判断兑换类型
  // const [password,setPassword ] = useState(''); // 判断兑换类型

  const router = useRouter();

  useEffect(() => {
    // console.log(router.params.type, 'type');
  }, []);

  function handleText(e) {
    setNickname(e.target.value.trim());
  }
  function handleTexts(e) {
    setPassword(e.target.value.trim());
  }
  function editpassword () {
    Taro.redirectTo({
      url: "/subPackageWork/pages/edit-password/index"
    })
  }
  function convert() {
    if (!password) {
      Taro.showToast({
        title: '请输入账号密码',
        icon: 'none',
      });
    } else {
      couponBind({
        coupon_no:Taro.getStorageSync('coupon_no'),
        exchange_no: password
      }).then(res => {
        Taro.redirectTo({
          url: `/subPackageWork/pages/coupon-list/index?sku_id=${res.sku_id}&&coupon_no=${Taro.getStorageSync('coupon_no')}`
        });
      });
    }
  }

  return (
    <View className='exchange' style={`margin-top: ${navigationBarHeight}`}>
      <NavigationBar  text='优惠券' type="bindcard"/>

      {/* <Image className='bg' src={Bg} /> */}
      <View className='content'>
        {/* {router.params.type ? (
          <Image className='title' src={CouponTitle} />
        ) : (
          <Image className='title' src={Title} />
        )} */}
        <View className='title'>输入优惠券密码</View>
        {/* <View className='code-box'>
          <Input placeholder='请输入账号' className='code' onInput={handleText} />

        </View> */}
        <View className='code-boxs'>
          <Input placeholder='请输入密码' type="number" className='code' onInput={handleTexts} />

        </View>
        <View
          className={classnames({
            'submit-btn': true,
            'active-btn': password,
          })}
          onClick={convert}
        >
          提交
        </View>
        {/* <View
          className='submit-btn active-btn'
          onClick={editpassword}
        >
          修改密码
        </View> */}
        {/* {type !== 'coupon' && (
          <View className='tip'>
            <View className='tip-title'>兑换规则说明：</View>
            <View className='tip-content'>1.输入兑换卡上的兑换码，即可成功进入下单页面；</View>
            <View className='tip-content'>2.使用兑换码购买只能购买兑换卡指定商品；</View>
            <View className='tip-content'>3.只有一次使用机会</View>
          </View>
        )} */}
      </View>
      {/* {isTip ? <Tips onClose={() => setIstip(false)} /> : null} */}
    </View>
  );
}

Exchange.config = {
  navigationBarTitleText: '兑换',
};

export default Exchange;
